<?php

namespace backend\controllers;

use Yii;
use common\models\LinkType\LinkType;
use common\models\LinkType\LinkTypeSearch;
use common\models\FriendLink\FriendLink;
use common\models\FriendLink\FriendLinkSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\UploadedFile;
use kartik\file\FileInput;//图片上传
use zhongkefu\zhongkefu;
use common\models\adminlog\AdminLog;
/**
 * FriendLinkController implements the CRUD actions for FriendLink model.
 */
class FriendLinkController extends BackendController
{


    /**
     * Lists all FriendLink models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new FriendLinkSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single FriendLink model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
		$model = $this->findModel($id);
		Adminlog::addlog(yii::$app->user->id, "查看友情链接 ".$model->link_name);
        return $this->render('view', [
            'model' => $model,

        ]);
    }

    /**
     * Creates a new FriendLink model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */

	public function actionCreate()
    {
        $model = new FriendLink();
//上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {

            $model->link_logo = UploadedFile::getInstance($model, 'link_logo');

            if($model->link_logo){
                $siteRoot = Yii::$app->params['memberimg'];
                $tempPath = $_FILES['FriendLink']['tmp_name']['link_logo'];
                $filesName = uniqid() . '.' . pathinfo($_FILES['FriendLink']['name']['link_logo'], PATHINFO_EXTENSION);
                $uploadPath = $siteRoot . $filesName;
                move_uploaded_file($tempPath, $uploadPath);
                $answer = array('newname' => $filesName);
                $json = json_encode($answer);
                $model->link_logo = $json;
            }
            if($model->save()){
				$name = Yii::$app->request->post()['FriendLink']['link_name'];
				Adminlog::addlog(yii::$app->user->id, "添加友情链接 ".$name);
                return $this->redirect(['view', 'id' => $model->link_id]);
            }else{
                Yii::$app->session->setFlash('warning', Yii::t('app', '图片不能为空'));
                return $this->render('create', [
                    'model' => $model,
                ]);
            }
        } else {
            return $this->render('create', [
                'model' => $model,
				
            ]);
        }
    }

    /**
     * Updates an existing FriendLink model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
	
      $model = $this->findModel($id);
        //上传图片插件
        if ($model->load(Yii::$app->request->post()) ) {

            $model->link_logo = UploadedFile::getInstance($model, 'link_logo');

            if($model->link_logo){
                $siteRoot = Yii::$app->params['memberimg'];
                $tempPath = $_FILES['FriendLink']['tmp_name']['link_logo'];
                $filesName = uniqid() . '.' . pathinfo($_FILES['FriendLink']['name']['link_logo'], PATHINFO_EXTENSION);
                $uploadPath = $siteRoot . $filesName;
                move_uploaded_file($tempPath, $uploadPath);
                $answer = array('newname' => $filesName);
                $json = json_encode($answer);
                $model->link_logo = $json;
            }
			 if (!$model->link_logo) {
                $new = $this->findModel($id);
                $model->link_logo = $new->link_logo;
            }
            if($model->save()){
				$name = Yii::$app->request->post()['FriendLink']['link_name'];
				Adminlog::addlog(yii::$app->user->id, "修改友情链接 ".$name);
                return $this->redirect(['view', 'id' => $model->link_id]);
            }else{
                return $this->render('update', [
                    'model' => $model,
                ]);
            }

        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }


    /**
     * Deletes an existing FriendLink model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
		$model->delete();
		Adminlog::addlog(yii::$app->user->id, '删除友情链接 '.$model->link_name);
        return $this->redirect(['index']);
    }

    /**
     * Finds the FriendLink model based on its primary key value.
     * @param integer $id
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @return FriendLink the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = FriendLink::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /**
     * 删除按钮
     * If update is successful, the browser will be redirected to the 'index' page.
     * @return mixed
     */
    public function actionAlldelete()
    {
		$artname="";
        if(isset(zhongkefu::$app->request->post()["checkboxid"])){
            $ids = zhongkefu::$app->request->post()["checkboxid"];
            $ids = explode(',',$ids);
			foreach($ids as $val){
				$model = FriendLink::findOne(['link_id' => $val]);
				$artname .= $model->link_name.",";
			}
			$artname = substr($artname,0,-1);
            foreach($ids as $link_id){
                $model = FriendLink::findOne(['link_id' => $link_id]);
                $this->findModel($link_id)->delete();
            }
			Adminlog::addlog(yii::$app->user->id, '批量删除友情链接 '.$artname);
            return true;
        }
    }
	

}
